﻿using MailClient.Data.IndexTables;
using nBayes;

namespace MailClient.Handlers
{
    /// <summary>
    /// Bayesian spam detector for the mail that gets thru the primary filter
    /// </summary>
    public class spamdetect
    {
        #region Private
        private string subject
        {
            get;
            set;
        }
        private string body
        {
            get;
            set;
        }
        #endregion
        #region CTOR
        public spamdetect(string Subject, string Body)
        {
            subject = Subject;
            body = Body;
        }
        #endregion
        #region Procedures
        /// <summary>
        /// Checks the mail to see if it might be spam
        /// </summary>
        /// <returns>True if it is spam, False if it is not</returns>
        public bool TestMail()
        {
            // TEST IF THE MAIL MATCHES ANY OF MY SPAM FILTERS
            // RETURN TRUE IF IT IS SPAM OR FALSE IF IT IS OK

            Analyzer analyzer = new Analyzer();
            Entry document = Entry.FromString(subject + " " + body);
            CategorizationResult result = analyzer.Categorize(
                document, SpamIndex.Index, NotSpamIndex.Index);

            switch (result)
            {
                case CategorizationResult.First:
                    // is spam
                    SpamIndex.Index.Add(document);
                    return true;
                case CategorizationResult.Second:
                    //default:
                    //  is not spam
                    NotSpamIndex.Index.Add(document);
                    return false;
                case CategorizationResult.Undetermined:
                    // don't know
                    return false;
            }
            return false;
        }
        #endregion
    }
}
